View Javadoc
1 package xdoclet.gui.swing; 2 3 import org.apache.commons.logging.LogFactory; 4 5 import javax.swing.*; 6 7 import java.awt.Component; 8 import java.awt.GridLayout; 9 import java.awt.HeadlessException; 10 11 import java.beans.BeanInfo; 12 import java.beans.IntrospectionException; 13 import java.beans.Introspector; 14 import java.beans.PropertyDescriptor; 15 import java.beans.PropertyEditor; 16 import java.beans.PropertyEditorManager; 17 18 import java.lang.reflect.Method; 19 import java.io.File; 20 21 /*** 22 * 23 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a> 24 * @version $Revision: 1.7 $ 25 */ 26 class PropertySheet extends JPanel { 27 public PropertySheet(Object bean) { 28 setLayout(new GridLayout(0, 2)); 29 30 // Populate the sheet with components 31 BeanInfo beanInfo = null; 32 33 try { 34 beanInfo = Introspector.getBeanInfo(bean.getClass()); 35 } catch (IntrospectionException e) { 36 e.printStackTrace(); 37 throw new IllegalStateException("Couldn't get BeanInfo for " + bean.getClass() + ":" + e.getMessage()); 38 } 39 40 // JOptionPane.showMessageDialog(null,"PropertySheet Got BeanInfo for " + bean.getClass() + ":" + beanInfo.getClass().getName()); 41 // Set the name on this panel. Needed for CardLayout to work. 42 setName(beanInfo.getBeanDescriptor().getName()); 43 44 // Make property editors 45 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); 46 47 for (int i = 0; i < propertyDescriptors.length; i++) { 48 if (!propertyDescriptors[i].isHidden()) { 49 try { 50 Component editor = createEditorComponent(propertyDescriptors[i], bean); 51 52 JLabel label = new JLabel(propertyDescriptors[i].getDisplayName()); 53 54 label.setToolTipText(propertyDescriptors[i].getShortDescription()); 55 add(label); 56 add(editor); 57 } catch (IntrospectionException e) { 58 System.out.println(e.getMessage()); 59 } catch (HeadlessException e) { 60 e.printStackTrace(); //To change body of catch statement use Options | File Templates. 61 } 62 } 63 } 64 } 65 66 private Component createEditorComponent(PropertyDescriptor propertyDescriptor, Object bean) 67 throws IntrospectionException { 68 // Instantiate a new editor. 69 PropertyEditor propertyEditor = PropertyEditorManager.findEditor(propertyDescriptor.getPropertyType()); 70 71 if (propertyEditor == null) { 72 throw new IntrospectionException("No property editor found for type " 73 + propertyDescriptor.getPropertyType().getName()); 74 } 75 76 // Ask the bean for its current value. 77 Method getter = propertyDescriptor.getReadMethod(); 78 79 if (getter != null) { 80 try { 81 Object value = getter.invoke(bean, null); 82 83 propertyEditor.setValue(value); 84 } catch (Exception e) { 85 LogFactory.getLog(PropertySheet.class).error("Couldn't create editor", e); 86 JOptionPane.showMessageDialog(this,"Couldn't create editor: " + e.getMessage()); 87 throw new IntrospectionException(e.getMessage()); 88 } 89 90 // Now figure out how to display it... 91 if (propertyEditor.isPaintable() && propertyEditor.supportsCustomEditor()) { 92 // editorComponent = new PropertyCanvas(frame, editor); 93 return propertyEditor.getCustomEditor(); 94 } else if (propertyEditor.getTags() != null) { 95 // editorComponent = new PropertySelector(editor); 96 } else if (getter.getReturnType().equals(String.class)) { 97 return new PropertyText(propertyEditor, propertyDescriptor, bean); 98 } else if (getter.getReturnType().equals(File.class)) { 99 return new PropertyFile(propertyEditor, propertyDescriptor, bean); 100 } 101 } 102 throw new IntrospectionException("Property \"" + propertyDescriptor.getName() 103 + "\" has non-displayabale editor."); 104 } 105 }

This page was automatically generated by Maven